# _*_ coding utf-8 _*_
# george
# time: 2025/1/4上午9:34
# name: account.py
# comment:
from flask import Blueprint, render_template, request, redirect, session
from ..db import fetch_one, fetch_all
from ..func.common import update_status

# 创建蓝图对象
ac = Blueprint("account", __name__)


@ac.route("/")
@ac.route("/common/login", methods=["GET", "POST"])
def common_login():
    if request.method == "GET":
        return render_template("common_login.html")
    user_name = request.form.get("username")
    passwd = request.form.get("password")
    # 连接数据库进行数据库校验
    sql = "select * from users where username=%s and passwd=%s"
    user_dict = fetch_one(sql, (user_name, passwd))
    if user_dict:
        # 将用户信息存储到cookie里面去,在flask的内部会对数据进行加密,类似于加盐的操作
        session["user_info"] = {"id": user_dict.get("id"), "username": user_dict.get("username"),
                                "mobile": user_dict.get("mobile"), "role": user_dict.get("role")}
        if user_dict.get("role") == 1 or user_dict.get("role") ==0:  # 顾客
            return redirect("/common/home")  # 跳转到订单页面
        else:
            return "您暂时无权限登录该系统！！"
    return render_template("common_login.html", error="用户名或是密码错误!")


@ac.route('/common/home')
def common_home():
    user_info = session.get("user_info")
    # if user_info.get("role") == 0: # 管理员
    #     sql = " select * from orders left outer join users on orders.user_id=users.id"
    #     data_list = fetch_all(sql, ())
    #     data_list = update_status(data_list)
    # else:
    #     sql = "select * from orders left outer join users on orders.user_id=users.id where username=%s;"
    #     data_list = fetch_all(sql,(user_info["username"]),)
    #     data_list = update_status(data_list)
    sql = " select * from file left outer join team on file.team_id=team.id left outer join project on project_id=project.id"
    data_list = fetch_all(sql,())
    return render_template('common_home.html', data_list=data_list, user_info=user_info)


@ac.route("/common/team")
def common_team():
    sql = "select project from users where project != %s group by project;"
    team_list = fetch_all(sql, ("000",))
    team_list = [item.get("project") for item in team_list]
    user_info = session.get("user_info")
    return render_template("common_team.html", team_list=team_list, user_info=user_info)

@ac.route("/common/team/showfiles/<team>")
def common_team_showfiles(team):
    sql = " select * from file left outer join team on file.team_id=team.id left outer join project on project_id=project.id where team_name=%s"
    data_list_team = fetch_all(sql, (team,))
    sql = " select * from file left outer join team on file.team_id=team.id left outer join project on project_id=project.id where project_name=%s"
    data_list_project = fetch_all(sql, (team,))
    data_list = data_list_team or data_list_project
    user_info = session.get("user_info")
    return render_template("common_team_showfiles.html",data_list=data_list, user_info=user_info)



@ac.route("/common/project")
def common_project():
    sql = " select * from project"
    project_dict = fetch_all(sql,())
    project_list = [project["project_name"] for project in project_dict]
    user_info = session.get("user_info")
    return render_template("common_project.html", project_list=project_list,user_info=user_info)



